package com.supereye.platform.web.user.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.supereye.platform.base.BaseDao;
import com.supereye.platform.interfaces.BzClient;
import com.supereye.platform.web.user.model.TopUser;

@Repository
public class UserDao extends BaseDao {

	public void addUser(TopUser user) throws Exception{
		sqlSessionTemplate.insert("com.supereye.platform.web.user.dao.UserDao.addTopUser", user);
	}

	public List<TopUser> getUserList(Map<String,Object> params)  throws Exception{
		//sqlSessionTemplate.sele
		return sqlSessionTemplate.selectList("com.supereye.platform.web.user.dao.UserDao.queryUser", params);
	}

	public void deleteUsers(String[] userIds) throws Exception{
		List<Object[]> params = new ArrayList<Object[]>();
		for(String userId:userIds){
			params.add(new String[]{userId});
			
			BzClient.getClient().delUser(Integer.parseInt(userId));
		}
		jdbcTemplate.batchUpdate("delete from top_user where id=?", params);
	}
	
//	public List<SysUser> getUserByAccount(String account) throws Exception {
//		return jdbcTemplate.queryForList("sele",SysUser.class,new Object[]{});
//	}
//	RowCountCallbackHandler countCallback = new RowCountCallbackHandler();  // not reusable  
//	jdbcTemplate.query("select * from user", countCallback);  
//	int rowCount = countCallback.getRowCount();  
	
	public int getUserNumByAccount(String account) throws Exception {
		return jdbcTemplate.queryForObject("select count(*) from top_user where account=?", new Object[]{account}, Integer.class);
	}

	public void updateUser(TopUser user) throws Exception{
		sqlSessionTemplate.insert("com.supereye.platform.web.user.dao.UserDao.updateTopUser", user);
	}

	public void updateRelateId(TopUser user) throws Exception{
		sqlSessionTemplate.insert("com.supereye.platform.web.user.dao.UserDao.updateRelateId", user);
	}

	public List<String> getExpertsByUserAccount(String userAccount) throws Exception{
		return sqlSessionTemplate.selectList("com.supereye.platform.web.user.dao.UserDao.getExpertsByUserAccount", userAccount);
	}

	public List<String> getExpertsByUserId(String userId) throws Exception{
		return sqlSessionTemplate.selectList("com.supereye.platform.web.user.dao.UserDao.getExpertsByUserId", userId);

	}

	public TopUser getUserByAccount(String userAccount) throws Exception{
		return sqlSessionTemplate.selectOne("com.supereye.platform.web.user.dao.UserDao.getUserByAccount", userAccount);
	}

	public Integer getUserListCount(Map<String, Object> params) throws Exception{
		return sqlSessionTemplate.selectOne("com.supereye.platform.web.user.dao.UserDao.queryUserCount", params);
	}
	
	public String getExpertNamesById(String expertIds) throws Exception{
		expertIds = "("+expertIds+")";
		return sqlSessionTemplate.selectOne("com.supereye.platform.web.user.dao.UserDao.getExpertNamesById", expertIds);
	}
}
